//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: '关卡：A1',
    userInfo: {},
    position:{},
    hasUserInfo: false,
    lifted: false,
    levelClear: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    let grids = [[
      {row: 1,
       column: 1,
       value: "马",
      class: "horse"},
      {row: 1,
        column: 2,
        value: "",
        class: "non-horse"},
      {row: 1,
        column: 3,
        value: ""},
    ],
      [
        {row: 2,
          column: 1,
          value: "X"},
        {row: 2,
          column: 2,
          value: "X"},
        {row: 2,
          column: 3,
          value: ""},
      ],
      [
        {row: 3,
          column: 1,
          value: "出口"},
        {row: 3,
          column: 2,
          value: "X"},
        {row: 3,
          column: 3,
          value: ""},
      ],
    ]

    this.setData({position: grids})
    // 此处语法糖，不能这样写
    /*
    this.setData({this.data.position})
    this.setData({position: this.data.position})
     */

  },

  // 语法糖： function()不能省
  goHorse: function(e) {
    this.data.lifted = !this.data.lifted
    if (this.data.lifted) {
      let coordinates = e.target.id
      let horseRow = coordinates.substr(0,1)
      let horseColumn = coordinates.substr(2,1)
      console.log("点击了：" + horseRow + "," + horseColumn)
      let liftedHorse = false
      // debugger
      for (let i = 0; i<3; i++) {
        for (let j=0; j<3; j++) {
          let item = this.data.position[i][j]
          if (item.value == "马" && horseRow==i+1 && horseColumn==j+1){
            liftedHorse = true
          }
        }
      }
      if (liftedHorse) {
        this.refreshBoard(horseRow, horseColumn)
      } else {
        this.data.lifted = false
      }
    }

    if (!this.data.lifted) {
      this.validateMove(e)
    }
  },

  validateMove: function (e) {
    let coordinates = e.target.id
    let moveToRow = coordinates.substr(0,1)
    let moveToColumn = coordinates.substr(2,1)
    let grids = this.data.position
    let moveToPosition = grids[moveToRow-1][moveToColumn-1]
    if (moveToPosition.value == "移动") {
      this.clearHorse()
      moveToPosition.value= "马"
      this.setData({position: grids})
    } else if(moveToPosition.value == "过关") {
      this.clearHorse()
      this.setData({levelClear:true})
    }
  },

  clearHorse: function(){
    let grids = this.data.position
    for (let i = 0; i<3; i++) {
      for (let j=0; j<3; j++) {
        let item = this.data.position[i][j]
        if (item.value == "马" || item.value == "移动"){
          item.value = ""
        }
      }
    }
    this.setData({position: grids})
  },
  refreshBoard: function(horseRow, horseColumn) {
    if (horseRow == 1 && horseColumn == 1) {
      let grids = this.data.position
      grids[2-1][3-1].value = "移动"
      this.setData({position: grids})
    }
    if (horseRow == 2 && horseColumn == 3) {
      let grids = this.data.position
      grids[1-1][1-1].value = "移动"
      grids[3-1][1-1].value = "过关"
      this.setData({position: grids})
    }
  },

  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
